package com.nf.health.app.datamain.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.DialogOnitem;
import com.nf.health.app.customview.BottomSslideDialog;
import com.nf.health.app.models.Goal;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bluetools.BluetoothLeService;
import cn.bluetools.Pedometer_TypeInfo.Pedometer_Type;

public class BraceletGoalActivity extends BaseActivity {

	
	public static final String KEY_STEP_GOAL = "goal_steps";
	public static final String KEY_CALORIES_GOAL = "goal_calories";
	public static final String KEY_DISTANCE_GOAL = "goal_distance";
	public static final String KEY_SLEEP_GOAL = "goal_sleep";
	private RelativeLayout rl_steps;
	private RelativeLayout rl_calorie;
	private RelativeLayout rl_distance;
	private RelativeLayout rl_sleep;
	List<String> steps = new ArrayList<String>();
	ArrayList<String> distances = new ArrayList<String>();
	ArrayList<String> calories = new ArrayList<String>();
	ArrayList<String> sleep = new ArrayList<String>();
	private int index = 0;

	Goal goal = new Goal();
	private ServiceConnection mServiceConnection;
	private BluetoothLeService mBluetoothLeService; // 蓝牙服务类
	private LocalBroadcastManager localBroadcastManager;
	/**
	 * 接收蓝牙服务传入的事件消息，或者是接收到的蓝牙数据
	 */
	private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED
					.equals(action)) {
				switch (goal.getType()) {
				case HealthyBraceletActivity.TYPE_SET_SETP_GOAL:
//					6E01010D008F
					if(goal != null){
						mBluetoothLeService.setGoalSteps(goal.getValue());
					}
					break;
				case HealthyBraceletActivity.TYPE_SET_CALORIE_GOAL:
//					6E0101A2028F
					mBluetoothLeService.setGoalCal(goal.getValue());
					break;
				case HealthyBraceletActivity.TYPE_SET_DISTANCE_GOAL:
//					6E0101A2028F
					mBluetoothLeService.setGoalDis(goal.getValue());
					break;
				case HealthyBraceletActivity.TYPE_SET_SLEEP_GOAL:
					break;
				}
			} else if (BluetoothLeService.ACTION_GATT_SERVICES_TIMEOUT
					.equals(action)) {
				hideLoading();
			}
			else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
				hideLoading();
				// 蓝牙断开
			} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
				// 接收到蓝牙数据
				hideLoading();
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		step_goal = PreferenceHelper.getString(KEY_STEP_GOAL, "7000");
		calories_goal = PreferenceHelper.getString(KEY_CALORIES_GOAL, "350");
		distance_goal = PreferenceHelper.getString(KEY_DISTANCE_GOAL, "5");
		sleep_goal = PreferenceHelper.getString(KEY_SLEEP_GOAL, "8");
		setTitleBar(R.layout.layout_common_header);
		
		setContent(R.layout.activity_bracelet_goal);
		
		ViewUtil.setText(this, R.id.titlebar_title_tv, "目标设置");
		
		initView();
		
		new Thread(
				new Runnable() {
					@Override
					public void run() {
						for (int i = 1000; i <= 100000; i=i+1000) {
							steps.add(i+"");
						}
						for (int j = 1; j <= 24; j++) {
							sleep.add(j+"");
						}						
						for (int k = 1; k <= 100;k++) {
							distances.add(k+"");
						}						
						for (int l = 50; l <= 1000; l=l+50) {
							calories.add(l+"");
						}						
					}
				}).start();
		
		initBluetooth();
	}
	
	private void initBluetooth() {
		localBroadcastManager = LocalBroadcastManager.getInstance(this);
		
		mServiceConnection = new ServiceConnection() {
			@Override
			public void onServiceConnected(ComponentName componentName,
					IBinder service) {
				mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
				// BluetoothLeService.setPedometerType(Pedometer_TypeInfo.Pedometer_Type.L28S);
				BluetoothLeService.setPedometerType(Pedometer_Type.L28T);
			}

			@Override
			public void onServiceDisconnected(ComponentName componentName) {
				mBluetoothLeService = null;
			}
		};

		Intent bleService = new Intent(this, BluetoothLeService.class);
		bindService(bleService, mServiceConnection, Context.BIND_AUTO_CREATE);
		localBroadcastManager.registerReceiver(mGattUpdateReceiver, BluetoothLeService.makeGattUpdateIntentFilter());
	}

	private void initView() {
		rl_steps = ViewUtil.getView(this, R.id.rl_steps);
		rl_calorie = ViewUtil.getView(this, R.id.rl_calorie);
		rl_distance = ViewUtil.getView(this, R.id.rl_distance);
		rl_sleep = ViewUtil.getView(this, R.id.rl_sleep);
		
		tv_steps = ViewUtil.getView(this, R.id.tv_steps);
		tv_calories = ViewUtil.getView(this, R.id.tv_calories);
		tv_distance = ViewUtil.getView(this, R.id.tv_distance);
		tv_sleep = ViewUtil.getView(this, R.id.tv_sleep);
		
		rl_calorie.setOnClickListener(this);
		rl_steps.setOnClickListener(this);
		rl_distance.setOnClickListener(this);
		rl_sleep.setOnClickListener(this);
		
		tv_steps.setText(step_goal+"步");
		tv_calories.setText(calories_goal+"千卡");
		tv_distance.setText(distance_goal+"千米");
		tv_sleep.setText(sleep_goal+"小时");
	}
	
	DialogOnitem itemListener = new DialogOnitem() {
		@Override
		public void onItemClickListener(int position) {
			switch (index) {
			case 1:
				tv_steps.setText(steps.get(position)+"步");
				PreferenceHelper.putString(KEY_STEP_GOAL, steps.get(position));
				goal.setType(HealthyBraceletActivity.TYPE_SET_SETP_GOAL);
				goal.setValue(Integer.parseInt(steps.get(position)));
				
				break;
			case 2:
				tv_calories.setText(calories.get(position)+"千卡");
				PreferenceHelper.putString(KEY_CALORIES_GOAL, calories.get(position));
				goal.setType(HealthyBraceletActivity.TYPE_SET_CALORIE_GOAL);
				goal.setValue(Integer.parseInt(calories.get(position)));
				break;
			case 3:
				tv_distance.setText(distances.get(position)+"千米");
				PreferenceHelper.putString(KEY_DISTANCE_GOAL, distances.get(position));
				goal.setType(HealthyBraceletActivity.TYPE_SET_DISTANCE_GOAL);
				goal.setValue(Integer.parseInt(distances.get(position)));
				break;
			case 4:
				tv_sleep.setText(sleep.get(position)+"小时");
				PreferenceHelper.putString(KEY_SLEEP_GOAL, sleep.get(position));
				goal.setType(HealthyBraceletActivity.TYPE_SET_SLEEP_GOAL);
				goal.setValue(Integer.parseInt(sleep.get(position)));
				break;

			default:
				break;
			}
			if (mBluetoothLeService != null && BluetoothLeService.isConnected) {
				showLoading("设置中...");
				switch (goal.getType()) {
				case HealthyBraceletActivity.TYPE_SET_SETP_GOAL:
					mBluetoothLeService.setGoalSteps(goal.getValue());
					break;
				case HealthyBraceletActivity.TYPE_SET_CALORIE_GOAL:
					mBluetoothLeService.setGoalCal(goal.getValue());
					break;
				case HealthyBraceletActivity.TYPE_SET_DISTANCE_GOAL:
					mBluetoothLeService.setGoalDis(goal.getValue());
					break;
				case HealthyBraceletActivity.TYPE_SET_SLEEP_GOAL:
//					mBluetoothLeService.set
					hideLoading();
					//TODO 此固件暂未支持此方法
					break;
				}
			}else{
				showToast("蓝牙连接已断开");
			}
		}
	};
	private TextView tv_steps;
	private TextView tv_calories;
	private TextView tv_distance;
	private TextView tv_sleep;
	private String step_goal;
	private String calories_goal;
	private String distance_goal;
	private String sleep_goal;
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.rl_steps:
			index = 1;
			step_goal = PreferenceHelper.getString(KEY_STEP_GOAL, "7000");
			new BottomSslideDialog(this, steps.indexOf(step_goal),
					steps, itemListener).createPicker();
			break;
		case R.id.rl_calorie:
			index = 2;

			calories_goal = PreferenceHelper.getString(KEY_CALORIES_GOAL, "350");
			new BottomSslideDialog(this,calories.indexOf(calories_goal),
					calories, itemListener).createPicker();
			break;
		case R.id.rl_distance:
			index = 3;

			distance_goal = PreferenceHelper.getString(KEY_DISTANCE_GOAL, "5");
			new BottomSslideDialog(this, distances.indexOf(distance_goal),
					distances, itemListener).createPicker();
			break;
		case R.id.rl_sleep:
			index = 4;

			sleep_goal = PreferenceHelper.getString(KEY_SLEEP_GOAL, "8");
			new BottomSslideDialog(this, sleep.indexOf(sleep_goal),
					sleep, itemListener).createPicker();
			break;

		default:
			break;
		}
	}
}
